﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using DOAN.DO;
using DOAN.BL;

namespace DOAN
{
    public partial class Dangnhap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = -1;
            HttpRequest request = HttpContext.Current.Request;
            int x = Convert.ToInt32(request.QueryString["x"]);
            if (x == 1)
            {
                //Session.Timeout = 30000;
                Session["Tendangnhap"] = "";
                Session["hovaten"] = "";
                Session["quyen"] = "";
                Session["MaTK"] = "";
            }
            if (!IsPostBack)
            {
                testtk();
            }
        }

        protected void btDangnhap_Click(object sender, EventArgs e)
        {
            ThanhvienDO obj = new ThanhvienBL().Login(txtTendangnhap.Text, txtMatkhau2.Text);
            if (obj.Maquyen == null)
                Response.Write("<script>alert('Loi dang nhap')</script>");
            else
            {
                Session["Tendangnhap"] = obj.Tentaikhoan;
                Session["hovaten"] = obj.Hovaten;
                MasterPage mt = this.Master as MasterPage;
                LinkButton lb = mt.FindControl("LinkButton1") as LinkButton;
                lb.Visible = false;
                Label bl = mt.FindControl("lblDnthanhcong") as Label;
                bl.Visible = true;
                Label blten = mt.FindControl("lbtendn") as Label;
                blten.Visible = true;
                blten.Text = Session["Tendangnhap"].ToString();
                LinkButton lbDangky = mt.FindControl("lbDangky") as LinkButton;
                //lbDangky.Visible = false;
                LinkButton lbthoat = mt.FindControl("lbThoat") as LinkButton;
                lbthoat.Visible = true;
                testcookie();
                Session["quyen"] = obj.Maquyen;
                // Session.Timeout = 30;
                Session["MaTK"] = obj.Tentaikhoan;
                string s = obj.Maquyen;
                if (s == "AD" | s == "GV")
                    Response.Redirect("Admin/Admin.aspx");
                else
                    Response.Redirect("Default.aspx");
            }
        }

        private void testcookie()
        {
            if (Request.Browser.Cookies == true)
            {
                if (chkNhomk.Checked == true)
                {
                    HttpCookie ckaccount = new HttpCookie("account");
                    ckaccount["username"] = txtTendangnhap.Text;
                    ckaccount["userpass"] = txtMatkhau2.Text;
                    ckaccount.Expires.AddMonths(1);
                    Response.Cookies.Add(ckaccount);
                }
            }
        }
        private void testtk()
        {
            if (Request.Cookies["account"] != null | Convert.ToString(Request.Cookies["account"]) != "")
            {
                HttpCookie ckaccount = Request.Cookies["account"];
                txtTendangnhap.Text = ckaccount["username"].ToString();
                txtMatkhau2.Text = ckaccount["userpass"].ToString();
                if (ckaccount["username"] == "")
                {
                    chkNhomk.Checked = false;
                }
                else
                    chkNhomk.Checked = true;
            }
        }
        protected void link1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Dangky.aspx");
        }
        protected void link2_Click(object sender, EventArgs e)
        {
            pnDangnhap.Visible = false;
        }

        protected void DangKy_Click(object sender, EventArgs e)
        {

        }

        protected void QuenMK_Click(object sender, EventArgs e)
        {
            Response.Redirect("QuenMK.aspx");
        }
    }
}
